<html>
<head><meta charset="utf-8"><title>Compiler Informational unused Map · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html">Compiler Informational unused Map</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209251319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zac <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251319">(Sep 07 2020 at 02:05)</a>:</h4>
<p>Hey there, I'm newish to rust and just spent a little while debugging an issue where I was using map on an iter but the code in the map wasn't running. I discovered that it was because I wasn't using the result of the map and because maps are evaluated lazily that meant it never evaluated at all. What I really wanted was for_each instead. </p>
<p>This seems like the sort of thing the compiler could have informed me of (please correct me if I'm wrong) and I'd love for it to be included in the future. I can't seem to find an existing feature request for this, where's the best place to add one?</p>



<a name="209251367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251367">(Sep 07 2020 at 02:06)</a>:</h4>
<p>hmm, this should have been caught by <code>must_use</code> on <code>Map</code></p>



<a name="209251370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251370">(Sep 07 2020 at 02:06)</a>:</h4>
<p>yeah: <a href="https://doc.rust-lang.org/src/core/iter/adapters/mod.rs.html#809">https://doc.rust-lang.org/src/core/iter/adapters/mod.rs.html#809</a><br>
do you know how to reproduce the issue?</p>



<a name="209251384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zac <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251384">(Sep 07 2020 at 02:07)</a>:</h4>
<p>Hmm, I'll see if I can get an example going.</p>



<a name="209251386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251386">(Sep 07 2020 at 02:07)</a>:</h4>
<p>Too bad this can't be a <code>must_use</code> on all iterators. I can imagine that there are a lot of iterators in the wild that don't have this marking</p>



<a name="209251496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251496">(Sep 07 2020 at 02:10)</a>:</h4>
<p>I rarely use <code>for_each</code>, preferring a for loop instead. If you do that then this error becomes more obvious because you always expect iterators to go into a for loop or <code>collect</code> eventually</p>



<a name="209251563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251563">(Sep 07 2020 at 02:12)</a>:</h4>
<p>(or <code>sum()</code>, <code>fold()</code>, etc)</p>



<a name="209251757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zac <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251757">(Sep 07 2020 at 02:18)</a>:</h4>
<p>My original use case was a little complex, involving futures:</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">tx_original</span><span class="p">,</span><span class="w"> </span><span class="n">rx</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mpsc</span>::<span class="n">channel</span><span class="p">(</span><span class="mi">50</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_futures</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">object_urls</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">into_iter</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">url</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">tx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tx_original</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">s3_client</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">s3_client_original</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">bucket</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bucket_original</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">runtime</span><span class="p">.</span><span class="n">spawn</span><span class="p">(</span><span class="k">async</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">download_object</span><span class="p">(</span><span class="o">&amp;</span><span class="n">s3_client</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">bucket</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">url</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="k">match</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nb">Ok</span><span class="p">(</span><span class="n">content</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">tx</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="n">content</span><span class="p">).</span><span class="k">await</span><span class="p">.</span><span class="n">unwrap</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="nb">Err</span><span class="p">(</span><span class="n">err</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">error</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">json</span><span class="o">!</span><span class="p">({</span><span class="s">&quot;description&quot;</span>: <span class="s">&quot;Error downloading object&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;error&quot;</span>: <span class="nc">format</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">err</span><span class="p">)}))</span><span class="w"></span>
<span class="w">            </span><span class="p">};</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
</code></pre></div>


<p>Here's a cut-down version, where the compiler behavior suddenly makes much more sense, as I'm explicitly ignoring that it's not used: </p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;starting&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">values</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">vec</span><span class="o">!</span><span class="p">[</span><span class="s">&quot;a&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;b&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;c&quot;</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_results</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">values</span><span class="p">.</span><span class="n">into_iter</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">val</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;Value: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">val</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;done&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>I'm happy if the answer to this is "it's your fault for marking the variable as unused".</p>



<a name="209251836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251836">(Sep 07 2020 at 02:20)</a>:</h4>
<p>yeah, <code>let _x = ...</code> or more commonly <code>let _ = ...</code> is the recommended way to drop a <code>must_use</code> value</p>



<a name="209251851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251851">(Sep 07 2020 at 02:21)</a>:</h4>
<p>the name <code>results</code> is also misleading because you aren't dropping the results, you are dropping the iterator that would produce the results</p>



<a name="209251980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251980">(Sep 07 2020 at 02:25)</a>:</h4>
<p>clippy should lint against print!/println! inside map.</p>



<a name="209251981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Compiler%20Informational%20unused%20Map/near/209251981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zac <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Compiler.20Informational.20unused.20Map.html#209251981">(Sep 07 2020 at 02:25)</a>:</h4>
<p>Yep, my intention was not to use the future as I already had a channel for getting my results out, but in marking it deliberately unused I was also unintentionally telling the compiler to not worry about the fact that the map would never be evaluated. Cool, makes sense now. </p>
<p>Yeah <code>results</code> isn't the best, fair call.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>